{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Untitled\n",
        "\n",
        "## How to run DDD with CSDID in Stata\n",
        "\n",
        "Here I will show a small example, using the mpdta datafile.\n",
        "\n",
        "First the setup"
      ],
      "id": "e5b05ba8-b265-4716-af14-0d741ec20b61"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(Written by R.              )"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "File f1.dta will be replaced"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            ".."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            ".\n",
            "file f1.dta saved"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "Difference-in-difference with Multiple Time Periods\n",
            "\n",
            "                                                         Number of obs = 1,295\n",
            "Outcome model  : regression adjustment\n",
            "Treatment model: none"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "------------------------------------------------------------------------------"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "             | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
            "-------------+----------------------------------------------------------------\n",
            "g2004        |"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            " t_2003_2004 |  -.0211008    .041165    -0.51   0.608    -.1017827    .0595812\n",
            " t_2003_2005 |  -.1292921   .0454923    -2.84   0.004    -.2184553   -.0401288\n",
            " t_2003_2006 |  -.2074393   .0571321    -3.63   0.000     -.319416   -.0954625\n",
            " t_2003_2007 |  -.1355455   .0595128    -2.28   0.023    -.2521884   -.0189026\n",
            "-------------+----------------------------------------------------------------\n",
            "g2006        |\n",
            " t_2003_2004 |   .0054858   .0350106     0.16   0.875    -.0631337    .0741054\n",
            " t_2004_2005 |  -.0217113   .0381993    -0.57   0.570    -.0965805    .0531579\n",
            " t_2005_2006 |   -.024916   .0290084    -0.86   0.390    -.0817715    .0319395\n",
            " t_2005_2007 |  -.0544724   .0379769    -1.43   0.151    -.1289058    .0199611\n",
            "-------------+----------------------------------------------------------------\n",
            "g2007        |\n",
            " t_2003_2004 |   .0273227   .0273815     1.00   0.318    -.0263441    .0809894\n",
            " t_2004_2005 |  -.0117455   .0323164    -0.36   0.716    -.0750844    .0515935\n",
            " t_2005_2006 |  -.0344658    .031155    -1.11   0.269    -.0955285    .0265969\n",
            " t_2006_2007 |  -.0117583   .0322454    -0.36   0.715    -.0749581    .0514416\n",
            "------------------------------------------------------------------------------"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Control: Never Treated\n",
            "\n",
            "See Callaway and Sant'Anna (2021) for details\n",
            "File f2.dta will be replaced"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            ".."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "."
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            ".\n",
            "file f2.dta saved\n",
            "\n",
            "Difference-in-difference with Multiple Time Periods\n",
            "\n",
            "                                                         Number of obs = 1,205\n",
            "Outcome model  : regression adjustment\n",
            "Treatment model: none\n",
            "------------------------------------------------------------------------------\n",
            "             | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
            "-------------+----------------------------------------------------------------\n",
            "g2004        |\n",
            " t_2003_2004 |  -.0036454    .017134    -0.21   0.832    -.0372275    .0299367\n",
            " t_2003_2005 |   -.016655   .0232565    -0.72   0.474    -.0622369    .0289269\n",
            " t_2003_2006 |  -.0705129   .0266075    -2.65   0.008    -.1226626   -.0183631\n",
            " t_2003_2007 |  -.0725062   .0217586    -3.33   0.001    -.1151523   -.0298601\n",
            "-------------+----------------------------------------------------------------\n",
            "g2006        |\n",
            " t_2003_2004 |   -.000845   .0293484    -0.03   0.977    -.0583669    .0566768\n",
            " t_2004_2005 |   .0069579   .0178941     0.39   0.697    -.0281139    .0420296\n",
            " t_2005_2006 |   .0125415    .024053     0.52   0.602    -.0346015    .0596845\n",
            " t_2005_2007 |  -.0352532   .0212842    -1.66   0.098    -.0769695     .006463\n",
            "-------------+----------------------------------------------------------------\n",
            "g2007        |\n",
            " t_2003_2004 |   .0270388     .01238     2.18   0.029     .0027745    .0513031\n",
            " t_2004_2005 |   .0024533   .0111352     0.22   0.826    -.0193712    .0242778\n",
            " t_2005_2006 |   -.025611   .0216096    -1.19   0.236     -.067965     .016743\n",
            " t_2006_2007 |   -.042635   .0129499    -3.29   0.001    -.0680163   -.0172538\n",
            "------------------------------------------------------------------------------\n",
            "Control: Never Treated\n",
            "\n",
            "See Callaway and Sant'Anna (2021) for details"
          ]
        }
      ],
      "source": [
        "qui:ssc install frause, replace\n",
        "frause mpdta, clear\n",
        "** Assume two groups, based on lpop\n",
        "gen g1=lpop>3.3\n",
        "** Now use csdid to estimate the model, without covariates\n",
        "csdid lemp if g1==0, ivar(countyreal) time(year) gvar(first_treat) saverif(f1) replace\n",
        "csdid lemp if g1==1, ivar(countyreal) time(year) gvar(first_treat) saverif(f2) replace"
      ],
      "id": "07b7ca43-5e9f-4a04-964a-ad6eb5f95668"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now there are two files with the corresponding RIFs, so I can get\n",
        "aggregations for each separately"
      ],
      "id": "c5941cea-0f8a-4991-ace5-72af19358579"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(Written by R.              )\n",
            "\n",
            "------------------------------------------------------------------------------\n",
            "             | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
            "-------------+----------------------------------------------------------------\n",
            "         ATT |  -.0526544   .0223489    -2.36   0.018    -.0964575   -.0088513\n",
            "------------------------------------------------------------------------------\n",
            "file f1.dta saved\n",
            "(Written by R.              )\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "------------------------------------------------------------------------------\n",
            "             | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]\n",
            "-------------+----------------------------------------------------------------\n",
            "         ATT |     -.0328   .0094731    -3.46   0.001     -.051367    -.014233\n",
            "------------------------------------------------------------------------------\n",
            "file f2.dta saved"
          ]
        }
      ],
      "source": [
        "use f1, clear\n",
        "csdid_stats simple, save\n",
        "ren ATT ATT_g1\n",
        "save f1, replace\n",
        "\n",
        "use f2, clear\n",
        "csdid_stats simple, save\n",
        "ren ATT ATT_g2\n",
        "save f2, replace"
      ],
      "id": "836c5aa7-126e-4e3b-becf-b7a3ad1ec877"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Next I need to merge them together"
      ],
      "id": "64ab3147-657a-467d-84c9-415c546ea194"
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(Written by R.              )\n",
            "\n",
            "    Result                      Number of obs\n",
            "    -----------------------------------------\n",
            "    Not matched                           500\n",
            "        from master                       259  (_merge==1)\n",
            "        from using                        241  (_merge==2)\n",
            "\n",
            "    Matched                                 0  (_merge==3)\n",
            "    -----------------------------------------\n",
            "\n",
            "    Variable |        Obs        Mean    Std. dev.       Min        Max\n",
            "-------------+---------------------------------------------------------\n",
            "      ATT_g1 |        259   -.0526544    .3603682  -3.026657    2.10358\n",
            "      ATT_g2 |        241      -.0328    .1473684  -.5103483   .7988653"
          ]
        }
      ],
      "source": [
        "use f1, clear\n",
        "merge 1:1 countyreal using f2\n",
        "sum ATT_g1 ATT_g2"
      ],
      "id": "ad7af8b5-d765-4637-84eb-e7341171a84d"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now you have both files together, with different treatment effects. To\n",
        "create a table you can use the command csdid_rif, to compare both\n",
        "results, and make test for differences"
      ],
      "id": "0ed16375-b221-4b4b-a49a-a26e37f0ca69"
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "------------------------------------------------------------------------------\n",
            "             |               Robust\n",
            "             | Coefficient  std. err.      z    P>|z|     [95% conf. interval]\n",
            "-------------+----------------------------------------------------------------\n",
            "      ATT_g1 |  -.0526544   .0223489    -2.36   0.018    -.0964575   -.0088513\n",
            "      ATT_g2 |     -.0328   .0094731    -3.46   0.001     -.051367    -.014233\n",
            "------------------------------------------------------------------------------\n",
            "\n",
            " ( 1)  ATT_g1 - ATT_g2 = 0\n",
            "\n",
            "           chi2(  1) =    0.67\n",
            "         Prob > chi2 =    0.4134"
          ]
        }
      ],
      "source": [
        "csdid_rif ATT_g1 ATT_g2\n",
        "test ATT_g1= ATT_g2"
      ],
      "id": "dc674da3-6687-4d94-adeb-4b8a11f719f2"
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "name": "nbstata",
      "display_name": "Stata (nbstata)",
      "language": "stata"
    },
    "language_info": {
      "name": "stata",
      "file_extension": ".do",
      "mimetype": "text/x-stata",
      "version": "17"
    }
  }
}